<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TableGen
 *
 * @author vlad
 */
class TableGen extends DBGenerator {
    
    private $_table;
    
    public function __construct(SQLTable $struct, $path) {
        if (!empty($struct)) {$this->_table = $struct;}
        $this->AddPath($path."models/", 'models');
        $this->AddPath($path."pools/", 'pools');
        $this->AddPath($path."models/base/", 'modelsbase');
        $this->AddPath($path."pools/base/", 'poolsbase');
        
        return $this;
    }
    
    
    private function prepareClassModelName($className) {
        return "model_".$className;
    }
    
    private function prepareClassPoolName($className) {
        return "pool_".$className;
    }
    
    private function prepareFileModelName($fileName) {
        return "model_".$fileName;
    } 
    
    private function prepareFilePoolName($fileName) {
        return "pool_".$fileName;
    }
    
    public function Generate() {
        try {
            $this->HumanizeClassNames = false;
            if ($this->CreateAllPaths()) {
                
                // Models
                $className = $this->HumanizeClassName($this->prepareClassModelName($this->_table));
                $baseClassName = $this->HumanizeClassName($this->prepareClassModelName($this->AddBaseSuffix($this->_table)));
              
                $fileName = $this->prepareFileModelName($this->_table);
                $baseFileName = $this->AddBaseSuffix($fileName);
                
                $fullFileName = $this->GetPath('models').$fileName;
                $fullBaseFileName = $this->GetPath('modelsbase').$baseFileName;
                
                
                $columnsCode = "";
                
                foreach($this->_table->GetColumns() as $column) {
                    $ColumnGen = new ColumnGen($column);
                    $columnsCode .= $ColumnGen->Generate();
                }
                
                
                // craete base classes
                $fileContent = <<<EOF
class $className extends $baseClassName {

}

EOF;
                
                $fileBaseContent = <<<EOF
class $baseClassName {
    const TABLE_NAME = "{$this->_table}";
    
    public static function factory($db=null) {
        \$instance = new {$this->_table}();
        if(\$db){
           \$instance->setDatabase(\$db);
        }
        return \$instance;
    }
        
    public function GetPrimaryKeyName() {
        return "{$this->_table->GetPrimaryKeyName()}";
    }
$columnsCode
}

EOF;
                
                
                
                $this->CreatePHPFile($fullFileName, $fileContent);
                $this->CreatePHPFile($fullBaseFileName, $fileBaseContent);
                
                
                
                
                // Pools
                $className = $this->HumanizeClassName($this->prepareClassPoolName($this->_table));
                $baseClassName = $this->HumanizeClassName($this->prepareClassPoolName($this->AddBaseSuffix($this->_table)));
              
                $fileName = $this->prepareFilePoolName($this->_table);
                $baseFileName = $this->AddBaseSuffix($fileName);
                
                $fullFileName = $this->GetPath('pools').$fileName;
                $fullBaseFileName = $this->GetPath('poolsbase').$baseFileName;
                
                
                
                
                
                // craete base classes
                $fileContent = <<<EOF
class $className extends $baseClassName {
    
}

EOF;
                
                $fileBaseContent = <<<EOF
class $baseClassName {
    
}

EOF;
                
                
                
                $this->CreatePHPFile($fullFileName, $fileContent);
                $this->CreatePHPFile($fullBaseFileName, $fileBaseContent);
                
            }    
        } catch (Exception $e) {
            
        }
        
        return $this;
    } 
}

?>